package com.easybuy.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.easybuy.dao.BaseDao;
import com.easybuy.dao.NewsDao;
import com.easybuy.dao.Product_categoryDao;
import com.easybuy.entily.Product_category;
import com.easybuy.entily.User;


public class Product_categoryDaoImpl extends BaseDao implements Product_categoryDao{

	@Override
	public List<Product_category> getAll() {//�鿴һ������
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<Product_category> list=new ArrayList<Product_category>();
		Product_category pc=null;
		String sql="select * from easybuy_product_category where parentId=0";
		conn=super.getConnection();
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				pc=new Product_category();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
				pc.setIconClass(rs.getString("iconClass"));
				list.add(pc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<Product_category> getAll2(int parentId,int type) {//parentid=2
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<Product_category> list=new ArrayList<Product_category>();
		Product_category pc=null;
		String sql="select * from easybuy_product_category where parentId=? and type=?";
		conn=super.getConnection();
		try {
			pstmt=conn.prepareStatement(sql,parentId,type);
			rs=pstmt.executeQuery();
			while(rs.next()){
				pc=new Product_category();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
				pc.setIconClass(rs.getString("iconClass"));
				list.add(pc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<Product_category> getAll3(int parentId,int type) {//parentid=3
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<Product_category> list=new ArrayList<Product_category>();
		Product_category pc=null;
		String sql="select * from easybuy_product_category where parentId=?,type=?";
		conn=super.getConnection();
		try {
			pstmt=conn.prepareStatement(sql,parentId,type);
			rs=pstmt.executeQuery();
			while(rs.next()){
				pc=new Product_category();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
				pc.setIconClass(rs.getString("iconClass"));
				list.add(pc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int addNews(Product_category pro) {//���
		// TODO Auto-generated method stub
		String sql="INSERT INTO easybuy_product_category(name,parentId,type) VALUES(?,?,?)";
		Object []  obj={pro.getName(),pro.getParentId(),pro.getType()};
		return super.executeUpdate(sql, obj);
	}

	@Override
	public int updateNewsInfo(Product_category pca) {//�޸�
		// TODO Auto-generated method stub
		String sql="UPDATE easybuy_product_category SET name=?,parentId=?,type=? WHERE id=?)";
		Object []  obj={pca.getName(),pca.getParentId(),pca.getType(),pca.getId()};
		return super.executeUpdate(sql, obj);
	}

	@Override
	public int delNewsInfo(int id) {//ɾ��
		// TODO Auto-generated method stub
		
		String sql="DELETE FROM easybuy_product_category WHERE id=?";
		Object []  obj={id};
		return super.executeUpdate(sql, obj);
	}

	@Override
	public List<Product_category> All() {//��ѯ����
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<Product_category> list=new ArrayList<Product_category>();
		Product_category pc=null;
		String sql="select * from easybuy_product_category";
		conn=super.getConnection();
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				pc=new Product_category();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
				pc.setIconClass(rs.getString("iconClass"));
				list.add(pc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}



	@Override
	public List<Product_category> queryAllProductCategory(int parentid) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		Product_category pc=null;
		List<Product_category> list=new ArrayList<Product_category>();
		String sql="select * from easybuy_product_category where parentId=?";
		conn=super.getConnection();
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, parentid);
			rs=pstmt.executeQuery();
			while(rs.next()){
				pc=new Product_category();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
				pc.setIconClass(rs.getString("iconClass"));
				list.add(pc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	

}
